import collections


class Solution(object):
    def basicCalculatorIV(self, expression, evalvars, evalints):
        m = len(evalvars)
        d_map = {}
        for i in range(m):
            d_map[evalvars[i]] = evalints[i]
        op = {'+', '-', '*'}

        def dfs(s):
            st = s.split(' ')
            last = ['+']
            ans = []
            state = 0
            m = len(st)
            kuohao_st = []
            for i in range(m):
                word = st[i]
                if state == 0:
                    if word in op:
                        last.append(word)
                    else:
                        if word[0] == '(':
                            word = word[1:]
                            count = 1
                            kuohao_st.append(word)
                            while word[0] == '(':
                                word = word[1:]
                                count += 1
                            while word[-1] == ')':
                                word = word[:-1]
                                count -= 1
                            if count == 0:
                                kuohao_st[-1] = kuohao_st[-1][:-1]
                                state = 0
                                ans.append(dfs(' '.join(kuohao_st)))
                                kuohao_st = []
                            else:
                                state = 1
                        else:
                            if '0' <= word[0] <= '9':
                                ans.append({'#': int(word)})
                            else:
                                if word in d_map:
                                    ans.append({'#': d_map[word]})
                                else:
                                    ans.append({word: 1})
                else:
                    kuohao_st.append(word)
                    while word[0] == '(':
                        count += 1
                        word = word[1:]
                    while word[-1] == ')':
                        count -= 1
                        word = word[:-1]
                    if count == 0:
                        kuohao_st[-1] = kuohao_st[-1][: -1]
                        ans.append(dfs(' '.join(kuohao_st)))
                        kuohao_st = []
                        state = 0
            out = collections.defaultdict(int)
            state = 0
            n = len(last)
            for i in range(n - 1, -1, -1):
                if state == 0:
                    if last[i] == '*':
                        last_item = ans[i]
                        state = 1
                    else:
                        if last[i] == '+':
                            for key in ans[i]:
                                out[key] += ans[i][key]
                        else:
                            for key in ans[i]:
                                out[key] -= ans[i][key]
                else:
                    now_item = ans[i]
                    next_item = collections.defaultdict(int)
                    for key1 in now_item:
                        for key2 in last_item:
                            if key1 == '#':
                                if key2 == '#':
                                    next_item['#'] += now_item[key1] * last_item[key2]
                                else:
                                    next_item[key2] += now_item[key1] * last_item[key2]
                            else:
                                if key2 == '#':
                                    next_item[key1] += now_item[key1] * last_item[key2]
                                else:
                                    s1 = key1.split('*')
                                    s2 = key2.split('*')
                                    s1 += s2
                                    s1.sort()
                                    next_item['*'.join(s1)] += now_item[key1] * last_item[key2]
                    last_item = next_item
                    if last[i] == '*':
                        state = 1
                    else:
                        if last[i] == '+':
                            for key in last_item:
                                out[key] += last_item[key]
                        else:
                            for key in last_item:
                                out[key] -= last_item[key]
                        state = 0
            return out

        out = dfs(expression)
        changshu = out['#']
        out.pop('#')
        key_list = list(out.keys())
        key_list.sort(key=lambda x: (- len(x.split('*')), x))
        ans = []
        m = len(key_list)
        for i in range(m):
            if out[key_list[i]] != 0:
                ans.append(str(out[key_list[i]]) + '*' + key_list[i])
        if changshu != 0:
            ans.append(str(changshu))
        return ans


data = Solution()
# expression = "e + 8 - a + 5"
# evalvars = ["e"]
# evalints = [1]
# print(data.basicCalculatorIV(expression, evalvars, evalints))
expression = "a * b - b * a"
evalvars = []
evalints = []
print(data.basicCalculatorIV(expression, evalvars, evalints))
